package com.example.mongodblab.service;

import com.example.mongodblab.model.ContentLabel;
import jakarta.annotation.Resource;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class UpdateAfieldByBfield {
    @Resource
    MongoTemplate mongoTemplate;

    public void test() {
        ContentLabel contentLabel = new ContentLabel();


        contentLabel.setContentId("6000086");
        contentLabel.setLabelStatus(0);
        contentLabel.setClassStatus(0);

//        mongoTemplate.dropCollection(ContentLabel.class);
//
//        mongoTemplate.save(contentLabel);
//
//        List<ContentLabel> all = mongoTemplate.findAll(ContentLabel.class);
//        System.out.println(all);

        Query query = Query.query(Criteria.where("_id").is("6000086"));

        Update update = new Update();
        update.set("classStatus", 1);
        update.set("labelStatus", 1);
        update.set("hisClassStatus", new Document("$ifNull", Arrays.asList("$classStatus", null)));
        update.set("hisLabelStatus", new Document("$ifNull", Arrays.asList("$labelStatus", null)));

        System.out.println(update);

        Document[] updateArray = {new Document("$set", update.getUpdateObject())};


        mongoTemplate.updateFirst(query, update, ContentLabel.class);
    }
}
